1288. 删除被覆盖区间
为保证权益,题目请参考 1288. 删除被覆盖区间(From LeetCode).
解决方案1
Python
python
# 1288. 删除被覆盖区间
# https://leetcode-cn.com/problems/remove-covered-intervals/
################################################################################
from typing import List
from functools import cmp_to_key
class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
def myCmp(a,b):
if a[0] != b[0]:
return a[0] - b[0]
else:
return b[1] - a[1]
intervals.sort(key=cmp_to_key(myCmp))
tmpList = [intervals[-1][1]]
for i in range(len(intervals) - 1, 0, -1):
if tmpList[-1] <= intervals[i-1][1]:
while len(tmpList)!= 0 and tmpList[-1] <= intervals[i-1][1]:
tmpList.pop()
tmpList.append(intervals[i-1][1])
else:
tmpList.append(intervals[i-1][1])
return len(tmpList)
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.removeCoveredIntervals([[1,2],[1,4],[3,4]]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32